﻿using CRM.Infrastructure.Dto.Request.Org;
using CRM.Infrastructure.Dto.Response.Org;
using CRM.Service.Instance;
using CRM.Service.Interface;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using System;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;

namespace CRM.Api.Admin.Controllers
{/// <summary>
/// 部门
/// </summary>
    public class OrgController : BaseController
    {
        private readonly I_Org_Service _org_Service;
        public OrgController(I_Org_Service org_Service, I_Login_Service login_Service) : base(login_Service)
        {
            _org_Service= org_Service;
        }
        /// <summary>
        /// 获取部门列表
        /// </summary>
        /// <param name="isBan"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Get_Depts(int? isBan = -1)
        {
            var result = await _org_Service.Get_Depts(new Org_Request_Dto
            {
                isBan = isBan ?? -1
            });
            return Ok(result);
        }
        /// <summary>
        /// 获取部门下的用户
        /// </summary>
        /// <param name="id"></param>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Get_Dept_Users(string id, int? page = 1, int? limit = 10)
        {
            var result = await _org_Service.Get_Dept_Users(new Org_Request_Dto
            {
                id = id,
                page = page ?? 1,
                limit = limit ?? 10
            });
            return Ok(result);
        }
        /// <summary>
        /// 修改部门禁用状态
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Get_Ban(string id)
        {
            var result = await _org_Service.Get_Users(id);
            return Ok(result);
        }
        /// <summary>
        /// 删除部门
        /// </summary>
        /// <param name="id"></param>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Get_Del(string id)
        {
            var result = await _org_Service.Get_Del(id);
            return Ok(result);
        }
        /// <summary>
        /// 获取用户详情
        /// </summary>
        /// <param name="id"></param>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Get_Details(string id, int? page = 1, int? limit = 10)
        {
            var result = await _org_Service.Get_Details(id);
            return Ok(result);
        }
        /// <summary>
        /// 获取职务列表
        /// </summary>
        /// <param name="isBan"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Get_Duty(int? isBan = -1, int? page = 1, int? limit = 10)
        {
            var result = await _org_Service.Get_Duty(new Org_Request_Dto
            {
                isBan = isBan ?? -1,
                page = page ?? 1,
                limit = limit ?? 10
            });
            return Ok(result);
        }
        /// <summary>
        /// 获取职务详情
        /// </summary>
        /// <param name="id"></param>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Get_DutyDetails(string id,int? page = 1, int? limit = 10)
        {
            var result = await _org_Service.Get_DutyDetails(new Org_Request_Dto
            {
                id=id,
                page = page ?? 1,
                limit = limit ?? 10
            });
            return Ok(result);
        }
        /// <summary>
        /// 新增部门下用户
        /// </summary>
        /// <param name="name"></param>
        /// <param name="account"></param>
        /// <param name="duty"职务></param>
        /// <param name="dept"部门></param>
        /// <param name="desc"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Add_User(UserDept_Response_Dto dto)
        {
            var result = await _org_Service.Add_User(dto.name, dto.account, dto.duty, dto.dept, dto.desc, dto.gender);
            return Ok(result);
        }
        /// <summary>
        /// 新增部门
        /// </summary>
        /// <param name="name"></param>
        /// <param name="lv"></param>
        /// <param name="id"></param>
        /// <param name="check"></param>
        /// <param name="desc"></param>
        /// <param name="icon"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Add_Dept(Department_Response_Dto dto)
        {
            var result = await _org_Service.Add_Dept(dto.name, dto.dlv,dto.explain);
            return Ok(result);
        }
        /// <summary>
        /// 获取单个部门
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Get_Dept(string id)
        {
            var result = await _org_Service.Get_Dept(id);
            return Ok(result);
        }
        /// <summary>
        /// 新增职务
        /// </summary>
        /// <param name="name"></param>
        /// <param name="lv"></param>
        /// <param name="text"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Add_Duty(Department_Response_Dto dto)
        {
            var result = await _org_Service.Add_Duty(dto.name, dto.dlv, dto.explain);
            return Ok(result);
        }
        /// <summary>
        /// 获取职务下拉框
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Get_duty_xla()
        {
            var result = await _org_Service.Get_duty_xla();
            return Ok(result);
        }
        /// <summary>
        /// 修改部门
        /// </summary>
        /// <param name="name"></param>
        /// <param name="lv"></param>
        /// <param name="text"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Update_dept(Department_Response_Dto dto)
        {
            var result = await _org_Service.Update_dept(dto.id, dto.name, dto.dlv,dto.explain);
            return Ok(result);  
        }
        /// <summary>
        /// 修改职务
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Update_Duty(Department_Response_Dto dto)
        {
            var result = await _org_Service.Update_Duty(dto.id, dto.name, dto.dlv, dto.explain,dto.isBan);
            return Ok(result);
        }
    }
}
